package cn.itsource.org.service.impl;

import cn.itsource.basic.query.BaseQuery;
import cn.itsource.basic.query.PageList;
import cn.itsource.org.domain.Department;
import cn.itsource.org.mapper.DepartmentMapper;
import cn.itsource.org.service.IDepartmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DepartmentServiceImpl implements IDepartmentService {

    /**
     * 注入持久层
     */
    @Autowired
    private DepartmentMapper mapper;

    @Override
    public PageList<Department> getAllByPage(BaseQuery query) {
        // 1. 调用分页工具类设置分页参数
        PageHelper.startPage(query.getPageNum(),query.getPageSize());
        // 2. 执行查询操作
        List<Department> list = mapper.selectAll();
        // 3. 使用pageinfo封装查询出来数据
        PageInfo<Department> pageInfo = new PageInfo<>(list);
        // 4. 使用自己的分页工具类封装分页数据
        PageList<Department> pageList = new PageList<>(pageInfo.getTotal(), pageInfo.getList());
        // 5. 返回数据
        return pageList;
    }

    @Override
    public void delete(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void saveOrUpdate(Department department) {
        if(department.getId() == null) {
            // 说明id是空的，就是新增操作
            mapper.insertSelective(department);
        } else {
            // 修改操作
            mapper.updateByPrimaryKeySelective(department);
        }
    }
}
